﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Easyleave.Logic.Domain;
using System.Configuration;
using System.ServiceModel;
using Easyleave.Logic;
using System.Data.Linq;
using Easyleave.Logic.Membership;
using Easyleave.Logic.Data;

namespace Easyleave.Workflow
{
    public class LeaveDataManager
    {
        EasyleaveDataContext context;
        MembershipImpl membership;
        public LeaveDataManager() 
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            context = new EasyleaveDataContext(connectionString);
            membership = new MembershipImpl();
        }

        public void UpdateCurrentState(Guid instanceId, string stateName)
        {
            LeaveRecord record = (from leaveRec in context.LeaveRecords where leaveRec.workflowInstanceId == instanceId select leaveRec).First();
            record.currentState = stateName;
            context.SubmitChanges();
        }

        public void UpdateNextRecommenderId(Guid instanceId, Guid? newRecommenderId)
        {
            LeaveRecord record = (from leaveRec in context.LeaveRecords where leaveRec.workflowInstanceId == instanceId select leaveRec).First();
            record.nextRecommenderId = newRecommenderId;
            context.SubmitChanges();
        }


        public IList<LeaveDto> GetRecordsByRecommender(Guid? recommenderId)
        {
            var records = from leaveRec in context.LeaveRecords 
                          where leaveRec.nextRecommenderId == recommenderId
                          && StateNames.submittedState.ToString().Equals(leaveRec.currentState)
                          select leaveRec;
            List<LeaveDto> results = new List<LeaveDto>();
            foreach (LeaveRecord record in records)
            {
                results.Add(ConvertTypes(record));
            }
            return results;
        }

        public IList<LeaveDto> GetRecordsByApprover(Guid? approverId)
        {
            var records = from leaveRec in context.LeaveRecords 
                          where leaveRec.approverId == approverId
                          && StateNames.recommendedState.ToString().Equals(leaveRec.currentState)
                          select leaveRec;
            List<LeaveDto> results = new List<LeaveDto>();
            foreach (LeaveRecord record in records)
            {
                results.Add(ConvertTypes(record));
            }
            return results;
        }


        public IList<LeaveDto> GetRecordsByOwner(Guid? ownerId)
        {
            var records = from leaveRec in context.LeaveRecords where leaveRec.ownerId == ownerId select leaveRec;
            List<LeaveDto> results = new List<LeaveDto>();
            foreach (LeaveRecord record in records)
            {
                results.Add(ConvertTypes(record));
            }
            return results;
        }

        public LeaveDto GetRecord(Guid instanceId) 
        {
            LeaveRecord record = (from leaveRec in context.LeaveRecords where leaveRec.workflowInstanceId == instanceId select leaveRec).First();
            return ConvertTypes(record);
        }

        public void InsertRecord(LeaveDto leave)
        {
            context.LeaveRecords.InsertOnSubmit(ConvertTypes(leave));
            context.SubmitChanges();            
        }

        private LeaveDto ConvertTypes(LeaveRecord leaveRec)
        {
            return new LeaveDto()
            {
                CurrentStateName = leaveRec.currentState,
                NextRecommender = GetUser(leaveRec.nextRecommenderId),
                Owner = GetUser(leaveRec.ownerId),
                FromDate = leaveRec.fromDate,
                ToDate = leaveRec.todate,
                LeaveType = leaveRec.leaveType,
                WorkflowInstanceId = leaveRec.workflowInstanceId,
                Approver = GetUser(leaveRec.approverId),
                LeaveDays = leaveRec.daysCount
            };
        }

        private UserDto GetUser (Guid? userId) 
        {
            return membership.GetUser(userId);
        }

        private LeaveRecord ConvertTypes(LeaveDto leaveDto)
        {
            return new LeaveRecord() { 
                currentState = leaveDto.CurrentStateName,
                nextRecommenderId = leaveDto.NextRecommender.Id,
                ownerId = leaveDto.Owner.Id,
                workflowInstanceId = leaveDto.WorkflowInstanceId,
                approverId = leaveDto.Approver.Id,
                leaveType = leaveDto.LeaveType,
                fromDate =leaveDto.FromDate,
                todate = leaveDto.ToDate,
                daysCount = leaveDto.LeaveDays
            };
        }



    }
}
